package com.lagou.service.impl;

import com.lagou.dao.LagouTokenDao;
import com.lagou.pojo.LagouToken;
import com.lagou.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
 * @Author : liuchangling
 * @Descrition :
 * @Date： Created in 2:39 下午 2022/1/31
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private LagouTokenDao lagouTokenDao;

    @Override
    public LagouToken register(String email, String password) {
        String uuid = UUID.randomUUID().toString();
        LagouToken lagouToken = new LagouToken();
        lagouToken.setEmail(email);
        lagouToken.setToken(uuid);
        lagouToken.setPassword(password);
        LagouToken token = lagouTokenDao.save(lagouToken);
        return token;
    }

    @Override
    public boolean isRegistered(String email) {
        LagouToken lagouToken = lagouTokenDao.findFirstByEmail(email);
        return lagouToken != null;
    }

    @Override
    public LagouToken login(String email, String password) {
        LagouToken lagouToken = lagouTokenDao.findFirstByEmailAndPassword(email, password);
        if (lagouToken == null) {
            System.out.println("账户或密码错误");
            return null;
        }
        String uuid = UUID.randomUUID().toString();
        lagouToken.setToken(uuid);
        LagouToken token = lagouTokenDao.save(lagouToken);
        return token;
    }

    @Override
    public LagouToken info(String token) {
        return lagouTokenDao.findFirstByToken(token);
    }
}
